package com.huibur.devicecheck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huibur.devicecheck.enums.DeviceCategoryEnums;
import com.huibur.devicecheck.exception.DeviceCategoryNameRepeatException;
import com.huibur.devicecheck.exception.TypeNameRepeatException;
import com.huibur.devicecheck.mapper.DeviceCategoryMapper;
import com.huibur.devicecheck.model.param.DeviceCategoryParam;
import com.huibur.devicecheck.model.po.DeviceCategoryPO;
import com.huibur.devicecheck.model.query.DeviceCategoryQuery;
import com.huibur.devicecheck.model.vo.DeviceCategoryVO;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.request.SessionContext;
import com.huibur.devicecheck.service.IDeviceCategoryService;
import com.huibur.devicecheck.utils.ConvertPageUtil;
import com.huibur.devicecheck.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*******************************************************
* @title: DeviceCategoryServiceImpl.java
* @description: 
* @author wushiqi
* @date 2021/10/18
* @version 1.0
*******************************************************/
@Service
public class DeviceCategoryServiceImpl extends ServiceImpl<DeviceCategoryMapper, DeviceCategoryPO> implements IDeviceCategoryService {

    @Autowired
    DeviceCategoryMapper deviceCategoryMapper;
    /**
     * 新增设备名称
     * @param deviceCategoryParam
     * @return
     */
    @Override
    public ObjRes<Void> insertDeviceCategory(DeviceCategoryParam deviceCategoryParam) {
        DeviceCategoryPO deviceCategoryPO = deviceCategoryMapper.selectOne(new LambdaQueryWrapper<DeviceCategoryPO>()
                .eq(DeviceCategoryPO::getName, deviceCategoryParam.getName())
                .ne(DeviceCategoryPO::getIsDeleted, DeviceCategoryEnums.DELETE.getCode()));
        if (null != deviceCategoryPO) {
            throw new DeviceCategoryNameRepeatException();
        }
        DeviceCategoryPO deviceCategory = new DeviceCategoryPO();
        deviceCategory.setName(deviceCategoryParam.getName());
        deviceCategory.setTypeId(deviceCategoryParam.getTypeId());
        deviceCategory.setUnit(deviceCategoryParam.getUnit());
        deviceCategory.setIsDeleted(DeviceCategoryEnums.NORMAL.getCode());
        deviceCategory.setCreateAt(DateUtils.getCurrentTimestamp());
        deviceCategory.setCreateBy(SessionContext.getCurrentUserSubject().getUserId());
        deviceCategoryMapper.insert(deviceCategory);
        return new ObjRes<Void>().success("创建成功");
    }

    /**
     * 删除设备名称
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Long id) {
        DeviceCategoryPO deviceCategoryPO = new DeviceCategoryPO();
        deviceCategoryPO.setId(id);
        deviceCategoryPO.setIsDeleted(DeviceCategoryEnums.DELETE.getCode());
        deviceCategoryMapper.updateById(deviceCategoryPO);
        return true;
    }

    /**
     * 编辑设备名称
     * @param id
     * @param deviceCategoryParam
     * @return
     */
    @Override
    public ObjRes<Void> updateDeviceCategory(Long id, DeviceCategoryParam deviceCategoryParam) {
        DeviceCategoryPO deviceCategoryPO = deviceCategoryMapper.selectOne(new LambdaQueryWrapper<DeviceCategoryPO>()
                .ne(DeviceCategoryPO::getId, id)
                .eq(DeviceCategoryPO::getName, deviceCategoryParam.getName())
                .ne(DeviceCategoryPO::getIsDeleted, DeviceCategoryEnums.DELETE.getCode()));
        if (null != deviceCategoryPO) {
            throw new DeviceCategoryNameRepeatException();
        }
        DeviceCategoryPO deviceCategory = new DeviceCategoryPO();
        deviceCategory.setName(deviceCategoryParam.getName());
        deviceCategory.setTypeId(deviceCategoryParam.getTypeId());
        deviceCategory.setUnit(deviceCategoryParam.getUnit());
        deviceCategory.setIsDeleted(DeviceCategoryEnums.NORMAL.getCode());
        deviceCategory.setUpdateAt(DateUtils.getCurrentTimestamp());
        deviceCategory.setUpdateBy(SessionContext.getCurrentUserSubject().getUserId());
        deviceCategoryMapper.update(deviceCategory,new LambdaQueryWrapper<DeviceCategoryPO>().eq(DeviceCategoryPO::getId,id));
        return new ObjRes<Void>().success("修改成功");
    }

    /**
     * 设备名称列表(查询并分页)
     * @param deviceCategoryQuery
     * @return
     */
    @Override
    public PageRes<DeviceCategoryVO> selectListDeviceCategory(DeviceCategoryQuery deviceCategoryQuery) {
        Page<DeviceCategoryPO> page = new Page<>(deviceCategoryQuery.getCurrent(), deviceCategoryQuery.getPageSize());
        IPage<DeviceCategoryVO> pages = deviceCategoryMapper.selectListDeviceCategory(page,deviceCategoryQuery);
        return ConvertPageUtil.convertPage(deviceCategoryQuery.getCurrent(),page.getTotal(),pages.getRecords());
    }

    /**
     * 获取设备名称详情
     * @param id
     * @return
     */
    @Override
    public DeviceCategoryVO selectDeviceCategoryById(Long id) {
        DeviceCategoryPO deviceCategoryPO = deviceCategoryMapper.selectOne(new LambdaQueryWrapper<DeviceCategoryPO>()
                .eq(DeviceCategoryPO::getId, id));
        DeviceCategoryVO deviceCategoryVO = new DeviceCategoryVO();
        deviceCategoryVO.setId(deviceCategoryPO.getId());
        deviceCategoryVO.setName(deviceCategoryPO.getName());
        deviceCategoryVO.setTypeId(deviceCategoryPO.getTypeId());
        deviceCategoryVO.setUnit(deviceCategoryPO.getUnit());
        deviceCategoryVO.setIsDeleted(DeviceCategoryEnums.NORMAL.getCode());
        deviceCategoryVO.setCreateAt(DateUtils.getCurrentTimestamp());
        return deviceCategoryVO;
    }
}
